#include "transactionstab.h"
#include "ui_transactionstab.h"

#include "cashplusplus.h"
#include "datetimedelegate.h"
#include "currencydelegate.h"

TransactionsTab::TransactionsTab(QWidget *parent) :
	Tab(parent),
	m_ui(new Ui::TransactionsTab)
{
	m_ui->setupUi(this);
	m_model = 0;
	m_accountModel = 0;
}

TransactionsTab::~TransactionsTab()
{
	cleanup();
	delete m_ui;
}

void TransactionsTab::updateContents()
{
	initializeModel();
}

void TransactionsTab::flushData()
{
	if (m_model)
	{
		m_model->submitAll();
	}
}

void TransactionsTab::cleanup()
{
	if (m_model)
	{
		m_model->submitAll();
		delete m_model;
		m_model = 0;
	}

	if (m_accountModel)
	{
		delete m_accountModel;
		m_accountModel = 0;
	}
}

void TransactionsTab::initializeModel()
{
	cleanup();

	m_model = new TransactionModel(this);
	m_ui->transactionView->setModel(m_model);

	// Custom delegates for currency and date/time
	m_ui->transactionView->setItemDelegateForColumn(3, new DateTimeDelegate(m_ui->transactionView));
	m_ui->transactionView->setItemDelegateForColumn(2, new CurrencyDelegate(m_ui->transactionView));
	m_ui->transactionView->setItemDelegateForColumn(1, new QSqlRelationalDelegate(m_ui->transactionView));
	m_ui->transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);

	// Hide the ID and account columns
	m_ui->transactionView->setColumnHidden(0, true);
	m_ui->transactionView->setColumnHidden(1, true);

	m_accountModel = new AccountModel(this);
	m_accountModel->setSort(1, Qt::AscendingOrder);

	m_ui->accountFilter->setModel(m_accountModel);
	m_ui->accountFilter->setModelColumn(1);

	disableButtonsIfNecessary();
}

void TransactionsTab::on_addTransactionButton_clicked()
{
	m_model->addTransaction(currentAccount());
}

void TransactionsTab::on_deleteTransactionButton_clicked()
{
	QModelIndexList currentSelection = m_ui->transactionView->selectionModel()->selectedIndexes();

	if (currentSelection.count() > 0 && QMessageBox::Yes == QMessageBox::question(this, "Delete Transactions(s)?", "Delete selected transaction(s)?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No))
	{
		m_model->deleteTransactions(currentSelection);
	}
}

int TransactionsTab::currentAccount() const
{
	return m_ui->accountFilter->model()->index(m_ui->accountFilter->currentIndex(), 0).data().toInt();
}

void TransactionsTab::on_accountFilter_currentIndexChanged(int)
{
	m_model->setFilter(QString("account_id=%1").arg(currentAccount()));
	disableButtonsIfNecessary();
}

void TransactionsTab::disableButtonsIfNecessary()
{
	bool haveAccounts = m_ui->accountFilter->count() > 0;
	m_ui->accountFilter->setEnabled(haveAccounts);

	if (m_model->isNDAccount(currentAccount()))
	{
		m_ui->transactionView->setEditTriggers(QAbstractItemView::NoEditTriggers);
		m_ui->addTransactionButton->setEnabled(false);
		m_ui->deleteTransactionButton->setEnabled(false);
	}
	else
	{
		m_ui->transactionView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed | QAbstractItemView::AnyKeyPressed);
		m_ui->addTransactionButton->setEnabled(haveAccounts);
		m_ui->deleteTransactionButton->setEnabled(haveAccounts);
	}
}

void TransactionsTab::slot_updateNDTransactions(const QList<QStringList>& transactions)
{
	m_model->submitAll();
	m_accountModel->submitAll();
	QPair<int,int> ndAccounts = m_accountModel->ndAccounts();
	m_model->addNDTransactions(transactions, ndAccounts.first, ndAccounts.second);
}

void TransactionsTab::changeEvent(QEvent *e)
{
	QWidget::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		m_ui->retranslateUi(this);
		break;
	default:
		break;
	}
}
